/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	GraphicsTypesMapping.cpp
*
*	Comments	-	See GraphicsTypesMapping.h
*
**************************************************************************************/
#include "../Include/GraphicsTypesMapping.h"
#include "../Include/Tokenizer.h"

namespace Pulse
{
	EFillMode::Type GraphicsTypesMapping::ToFillMode( const CHAR *pType )
	{
		String type = pType;

		if ( type == PSX_String("Solid") )
		{
			return EFillMode::SOLID;
		}
		else if ( type == PSX_String("Wireframe") )
		{
			return EFillMode::WIREFRAME;
		}
		else
		{
			return EFillMode::_UNUSED_;
		}
	}

	ECullMode::Type GraphicsTypesMapping::ToCullMode( const CHAR *pType )
	{
		String type = pType;

		if ( type == PSX_String( "None" ) )
		{
			return ECullMode::NONE;
		}
		else if ( type == PSX_String("Front") )
		{
			return ECullMode::FRONT;
		}
		else if ( type == PSX_String("Back") )
		{
			return ECullMode::BACK;
		}
		else
		{
			return ECullMode::_UNUSED_;
		}
	}

	EBlendType::Type GraphicsTypesMapping::ToBlendType( const CHAR *pType )
	{
		String type = pType;

		if ( type == PSX_String("Zero") )
		{
			return EBlendType::ZERO;
		}
		else if ( type == PSX_String("One") )
		{
			return EBlendType::ONE;
		}
		else if ( type == PSX_String("Src_Color") )
		{
			return EBlendType::SRC_COLOR;
		}
		else if ( type == PSX_String("Inv_Src_Color") )
		{
			return EBlendType::INV_SRC_COLOR;
		}
		else if ( type == PSX_String("Src_Alpha") )
		{
			return EBlendType::SRC_ALPHA;
		}
		else if ( type == PSX_String("Inv_Src_Alpha") )
		{
			return EBlendType::INV_SRC_ALPHA;
		}
		else if ( type == PSX_String("Dest_Alpha") ) 
		{
			return EBlendType::DEST_ALPHA;
		}
		else if ( type == PSX_String("Inv_Dest_Alpha") )
		{
			return EBlendType::INV_DEST_ALPHA;
		}
		else if ( type == PSX_String("Dest_Color") )
		{
			return EBlendType::DEST_COLOR;
		}
		else if ( type == PSX_String("Inv_Dest_Color") )
		{
			return EBlendType::INV_DEST_COLOR;
		}
		else if ( type == PSX_String("Src_Alpha_Sat") )
		{
			return EBlendType::SRC_ALPHA_SAT;
		}
		else if ( type == PSX_String("Blend_Factor") )
		{
			return EBlendType::BLEND_FACTOR;
		}
		else if ( type == PSX_String("Inv_Blend_Factor") ) 
		{
			return EBlendType::INV_BLEND_FACTOR;
		}
		else if ( type == PSX_String("Src1_Color") )
		{
			return EBlendType::SRC1_COLOR;
		}
		else if ( type == PSX_String("Inv_Src1_Color") )
		{
			return EBlendType::INV_SRC1_COLOR;
		}
		else if ( type == PSX_String("Src1_Alpha") ) 
		{
			return EBlendType::SRC1_ALPHA;
		}
		else if ( type == PSX_String("Inv_Src1_Alpha") )
		{
			return EBlendType::INV_SRC1_ALPHA;
		}
		else
		{
			return EBlendType::_UNUSED_;
		}
	}

	EBlendOp::Type GraphicsTypesMapping::ToBlendOp( const CHAR *pType )
	{
		String type = pType;

		if ( type == PSX_String("Add") )
		{
			return EBlendOp::ADD;
		}
		else if ( type == PSX_String("Subtract") )
		{
			return EBlendOp::SUBTRACT;
		}
		else if ( type == PSX_String("Rev_Subtract") )
		{
			return EBlendOp::REV_SUBTRACT;
		}
		else if ( type == PSX_String("Min") )
		{
			return EBlendOp::MIN;
		}
		else if ( type == PSX_String("Max") )
		{
			return EBlendOp::MAX;
		}
		else
		{
			return EBlendOp::_UNUSED_;
		}
	}

	BYTE GraphicsTypesMapping::ToColorWriteMask( const CHAR *pMask )
	{
		BYTE value = 0;
		Tokenizer2 tokenizer( pMask );
		String token;

		tokenizer.AddSeparator( PSX_String('|') );
		tokenizer.AddSeparator( PSX_String(' ') );

		while ( tokenizer.NextToken( &token ) )
		{
			if ( token == PSX_String( "None" ) )
			{
				value = EColorWriteEnable::NONE;
				break;
			}
			else if ( token == PSX_String( "Red" ) )
			{
				value |= EColorWriteEnable::RED;
			}
			else if ( token == PSX_String( "Green" ) )
			{
				value |= EColorWriteEnable::GREEN;
			}
			else if ( token == PSX_String( "Blue" ) )
			{
				value |= EColorWriteEnable::BLUE;
			}
			else if ( token == PSX_String("Alpha") )
			{
				value |= EColorWriteEnable::ALPHA;
			}
			else if ( token == PSX_String("All") )
			{
				value = EColorWriteEnable::ALL;
				break;
			}
		}

		return value;
	}

	EDepthWriteMask::Type GraphicsTypesMapping::ToDepthWriteMask( const CHAR *pMask )
	{
		String mask = pMask;

		if ( mask == PSX_String("Zero") )
		{
			return EDepthWriteMask::ZERO;
		}
		else if ( mask == PSX_String("All") )
		{
			return EDepthWriteMask::ALL;
		}
		else
		{
			PSX_PushWarning("Invalid depth write mask. Returning EDepthWriteMask::ALL.");
			return EDepthWriteMask::ALL;
		}
	}

	EComparisonFunc::Type GraphicsTypesMapping::ToComparisonFunc( const CHAR *pFunc )
	{
		String func = pFunc;

		if ( func == PSX_String("Never") )
		{
			return EComparisonFunc::NEVER;
		}
		else if ( func == PSX_String("Less") )
		{
			return EComparisonFunc::LESS;
		}
		else if ( func == PSX_String("Equal") )
		{
			return EComparisonFunc::EQUAL;
		}
		else if ( func == PSX_String("Less_Equal") )
		{
			return EComparisonFunc::LESS_EQUAL;
		}
		else if ( func == PSX_String("Greater") )
		{
			return EComparisonFunc::GREATER;
		}
		else if ( func == PSX_String("Not_Equal") )
		{
			return EComparisonFunc::NOT_EQUAL;
		}
		else if ( func == PSX_String("Greater_Equal") )
		{
			return EComparisonFunc::GREATER_EQUAL;
		}
		else if ( func == PSX_String("Always") )
		{
			return EComparisonFunc::ALWAYS;
		}
		else
		{
			PSX_PushWarning( "Invalid comparison func. Returning EComparisonFunc::LESS" );
			return EComparisonFunc::LESS;
		}
	}

	U8 GraphicsTypesMapping::ToStencilReadMask( const CHAR *pMask )
	{
		String mask = pMask;

		if ( mask == PSX_String("Default") )
		{
			return EStencilReadMask::DEFAULT;
		}
		else
		{
			PSX_PushWarning("Invalid mask type. Returning EStencilReadMask::DEFAULT" );
			return EStencilReadMask::DEFAULT;
		}
	}

	U8 GraphicsTypesMapping::ToStencilWriteMask( const CHAR *pMask )
	{
		String mask = pMask;

		if ( mask == PSX_String("Default") )
		{
			return EStencilWriteMask::DEFAULT;
		}
		else
		{
			PSX_PushWarning("Invalid mask type. Returning EStencilWriteMask::DEFAULT" );
			return EStencilWriteMask::DEFAULT;
		}
	}

	EStencilOp::Type GraphicsTypesMapping::ToStencilOp( const CHAR *pOp )
	{
		String op = pOp;

		if ( op == PSX_String("Keep") )
		{
			return EStencilOp::KEEP;
		}
		else if ( op == PSX_String("Zero") )
		{
			return EStencilOp::ZERO;
		}
		else if ( op == PSX_String("Replace") )
		{
			return EStencilOp::REPLACE;
		}
		else if ( op == PSX_String("Inc_Sat") )
		{
			return EStencilOp::INCR_SAT;
		}
		else if ( op == PSX_String("Decr_Sat") )
		{
			return EStencilOp::DECR_SAT;
		}
		else if ( op == PSX_String("Invert") )
		{
			return EStencilOp::INVERT;
		}
		else if ( op == PSX_String("Incr") )
		{
			return EStencilOp::INCR;
		}
		else if ( op == PSX_String("Decr") )
		{
			return EStencilOp::DECR;
		}
		else
		{
			PSX_PushWarning("Invalid stencil op type. Returning EStencilOp::KEEP" );
			return EStencilOp::KEEP;
		}
	}

}